/* ----------------------------------------------------------------------------------------------------------------
 * ShapeRenderer
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.renderer 
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 图形渲染器
	 * @author Pure
	 * 
	 */
public class ShapeRenderer extends Renderer implements IShape
{
	public function ShapeRenderer(offsetX:Number = 0, offsetY:Number = 0)
	{
		super();
		
		_shape = new Shape();
		_offsetX = offsetX;
		_offsetY = offsetY;
	}
	
	
	
	
	// ----------------------------------［ Model ］----------------------------------

	protected var _shape:Shape;
	
	protected var _offsetX:Number, _offsetY:Number;
	
	
	// ----------------------------------［ Cache ］----------------------------------
	
	protected var _shapeDirty:Boolean;
	
	protected var _buffer:BitmapData;
	
	

	public function get graphics():Graphics
	{
		return _shape.graphics;
	}

	
	public function end():void
	{
		var w:Number = _shape.width;
		var h:Number = _shape.height;
		m_range = Math.sqrt(w * w + h * h) * 0.5;
		m_x = _offsetX + (w - _shape.x) * -0.5;
		m_y = _offsetY + (h - _shape.y) * -0.5;
		_advanceShape();
	}
	
	
	protected function _redrawBuffer():void
	{
		if (_buffer != null)
		{
			_buffer.dispose();
		}
		_buffer = new BitmapData(m_range * 2, m_range * 2, true, 0x0);
		_buffer.draw(_shape);
	}
	
	
	protected function _advanceShape():void
	{
		_shapeDirty = true;
		if (m_dirtyFlag > -1)  m_dirtyFlag = 2;
	}
	

		
	
	override protected function _generatePrimaryImage():void
	{
		if (_shapeDirty)
		{
			_redrawBuffer();
			_shapeDirty = false;
		}
	}
	
		
	
	override ns_despair function destroy():void
	{
		super.destroy();
		
		_buffer.dispose();
		_buffer = null;
		_shape = null;
	}
	
	
}

}